package callnumber.gtdev5.com.photoVault.adapter;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.bean.CallCustomBean;
import callnumber.gtdev5.com.photoVault.customview.TimeTextView;

/**
 * Created by cheng
 * PackageName FakeCall_4
 * 2018/8/17 17:15
 */

public class SettingAdapter extends BaseQuickAdapter<CallCustomBean, BaseViewHolder> implements TimeTextView.onTimeFinshListener {

    private int pos = -1;
    private String fromType;
    private List<CallCustomBean> datas;
    private CompoundButton.OnCheckedChangeListener listener;

    public SettingAdapter(int layoutResId, @Nullable List<CallCustomBean> data) {
        super(layoutResId, data);
        this.datas = data;

    }


    @Override
    protected void convert(BaseViewHolder helper, CallCustomBean item) {
        helper.setText(R.id.txt_time, item.getCall_Time());
        helper.setText(R.id.txt_userName, item.getCall_user());
        helper.addOnClickListener(R.id.lv_item);
        helper.addOnClickListener(R.id.img_delete);
        helper.setTag(R.id.img_delete, item.getId());
        Bundle bundle = new Bundle();
        bundle.putLong("id", item.getId());
        bundle.putInt("position", helper.getAdapterPosition());
        helper.setTag(R.id.swicth_time, bundle);
        if (listener != null) {
            helper.setOnCheckedChangeListener(R.id.swicth_time, listener);
        }
        TimeTextView timeTextView = helper.getView(R.id.txt_countdown);
        timeTextView.setPos(helper.getAdapterPosition());
        timeTextView.setOnTimeFinshListener(this);
        timeTextView.setTimes(item.getCall_Timeshap());

        if (helper.getAdapterPosition() == pos && fromType.equals("ItemLong")) {
            helper.setVisible(R.id.img_delete, true);
            helper.setVisible(R.id.swicth_time, false);
        }
        if (helper.getAdapterPosition() == pos && fromType.equals("hideImage")) {
            helper.setVisible(R.id.img_delete, false);
            helper.setVisible(R.id.swicth_time, true);
        }
        if (item.getIsCall() == 0) {
            helper.setChecked(R.id.swicth_time, true);
//            helper.setTextColor(R.id.txt_time,mContext.getResources().getColor(R.color.openTimeTextColor));
//            helper.setTextColor(R.id.txt_userName,mContext.getResources().getColor(R.color.openTimeTextColor));
//            helper.setTextColor(R.id.txt_daojishi,mContext.getResources().getColor(R.color.openTimeTextColor));
//            helper.setTextColor(R.id.txt_countdown,mContext.getResources().getColor(R.color.openTimeTextColor));
        } else {
            helper.setChecked(R.id.swicth_time, false);
//            helper.setTextColor(R.id.txt_time,mContext.getResources().getColor(R.color.item_setting_textColor));
//            helper.setTextColor(R.id.txt_userName,mContext.getResources().getColor(R.color.item_setting_textColor));
//            helper.setTextColor(R.id.txt_daojishi,mContext.getResources().getColor(R.color.item_setting_textColor));
//            helper.setTextColor(R.id.txt_countdown,mContext.getResources().getColor(R.color.item_setting_textColor));
            helper.setText(R.id.txt_countdown, "来电已关闭");
        }

    }


    public void deleImgShow(int pos, String formType) {
        this.fromType = formType;
        this.pos = pos;
        this.notifyDataSetChanged();
    }

    public void hideImage(int pos, String fromType) {
        this.fromType = fromType;
        this.pos = pos;
        this.notifyDataSetChanged();
    }

    public void deleteItem(int pos) {
        datas.remove(pos);
        this.notifyItemRemoved(pos);
    }

    public void setSwitchChangedListener(CompoundButton.OnCheckedChangeListener listener) {
        this.listener = listener;
    }

    public void setPosType(int pos, boolean checkedType) {
        this.datas.get(pos).setIsCall(checkedType ? 0 : 1);
        Handler handler = new Handler();
        Runnable r = new Runnable() {
            @Override
            public void run() {
                notifyItemChanged(pos);
            }
        };
        handler.post(r);
    }

    @Override
    public void timeFinish(int pos) {

    }
}
